﻿using System;
using System.Collections.Generic;
using System.Text;
using Top.Api.Stream.Connect;
using Top.Api.Stream.Message;
using System.Net;

namespace Top.Api.Stream
{
    public class TopCometStreamRequest
    {
        private string appkey;
        private string secret;
        private string userId;
        private string connectId;
        private IConnectionLifeCycleListener connectListener;
        private ITopCometMessageListener msgListener;

        public TopCometStreamRequest(string appkey, string secret, string userId, string connectId)
        {
            if (String.IsNullOrEmpty(appkey))
            {
                throw new Exception("appkey is null");
            }
            if (String.IsNullOrEmpty(secret))
            {
                throw new Exception("secret is null");
            }
            if (!String.IsNullOrEmpty(userId))
            {
                try
                {
                    long.Parse(userId);
                }
                catch (Exception e)
                {
                    throw new Exception("userid must a number type");
                }
            }
            if (String.IsNullOrEmpty(connectId))
            {
                this.connectId = getDefaultConnectId();
            }
            else
            {
                this.connectId = connectId;
            }
            this.appkey = appkey;
            this.secret = secret;
            this.userId = userId;

        }
        private static string getDefaultConnectId()
        {
            try
            {
                string strHostName = Dns.GetHostName(); //得到本机的主机名 
                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP  
                return ipEntry.AddressList[1].ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public string getAppkey()
        {
            return appkey;
        }
        public string getSecret()
        {
            return secret;
        }
        public string getUserId()
        {
            return userId;
        }

        public string getConnectId()
        {
            return connectId;
        }
        public IConnectionLifeCycleListener getConnectListener()
        {
            return connectListener;
        }
        public void setConnectListener(IConnectionLifeCycleListener connectListener)
        {
            this.connectListener = connectListener;
        }
        public ITopCometMessageListener getMsgListener()
        {
            return msgListener;
        }
        public void setMsgListener(ITopCometMessageListener msgListener)
        {
            this.msgListener = msgListener;
        }

        public int hashCode()
        {
            //final int prime = 31;
            int result = 1;
            //result = prime * result + ((appkey == null) ? 0 : appkey.hashCode());
            //result = prime * result
            //        + ((connectId == null) ? 0 : connectId.hashCode());
            //result = prime * result + ((userId == null) ? 0 : userId.hashCode());
            return result;
        }

        public bool equals(object obj)
        {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            //if (getClass() != obj.getClass())
            //    return false;
            TopCometStreamRequest other = (TopCometStreamRequest)obj;
            if (appkey == null)
            {
                if (other.appkey != null)
                    return false;
            }
            else if (!appkey.Equals(other.appkey))
                return false;
            if (connectId == null)
            {
                if (other.connectId != null)
                    return false;
            }
            else if (!connectId.Equals(other.connectId))
                return false;
            if (userId == null)
            {
                if (other.userId != null)
                    return false;
            }
            else if (!userId.Equals(other.userId))
                return false;
            return true;
        }
    }
}
